<template>
  <!-- 总容器 -->
    <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>我的预约</p>
            <div></div>
        </header>
        <div class="top-ban"></div>

        <ul>
            <li v-for="order, in orderList" :key="order.id">
                <div class="left">
                    <p>{{ order.orderDate }}</p>
                    <p>{{ order.setmealName }}</p>
                </div>
                <el-button  class="right" @click=handlerCancel(order.id,order.orderDate)>
                    取消预约
                </el-button>
            </li>
        </ul>
        
        <div class="bottom-ban"></div>
        <Footer/>
    </div>
    

</template>

<script setup>
import {listOrder,cancelOrder} from "@/api/order";
import { formatDate } from "@/utils/date";
import { ElMessage } from "element-plus";


const currentDate = new Date();
const router = useRouter();

//取消预约
function handlerCancel(id,date){
    const orderDate=new Date(date)
    const permitDate=new Date()
    permitDate.setDate(currentDate.getDate() + 1);
    //如果预约的是明天，则无法取消
    if(orderDate< permitDate){
        ElMessage.error("预约无法取消");
        return;
    }else{
        cancelOrder(id).then(response=>{
            if (response.data.code == 1) {
                ElMessage.success("取消预约成功");
                router.push('/appointmentcancel')
            }
        })
        // alert('今天的日子是'+formatDate(currentDate))
        // alert('所以可以取消'+formatDate(permitDate)+'的预约')
    }
}

// 获取预约列表
const orderList = ref([])
function getOrderList(){
    listOrder().then(response=>{
        if (response.data.code == 1) {
            orderList.value = response.data.data
        }
    })
}

getOrderList()
</script>

<style scoped>
/*********************** 总容器 ***********************/
.wrapper{
    width: 100%;
    height: 100%;
    background-color: #F9F9F9;
}

/*********************** header ***********************/
header{
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}
header .fa{
    font-size: 8vw;
}

/*********************** common样式 ***********************/
.top-ban{
    width: 100%;
    height: 15.7vw;
}
.bottom-ban{
    width: 100%;
    height: 14.2vw;
}

/*********************** ul ***********************/
ul{
    width: 86vw;
    margin: 0 auto;
}
ul li{
    width: 100%;
    height: 14vw;
    border-bottom: solid 1px #EEE;

    display: flex;
    justify-content: space-between;
    align-items: center;
}
ul li .left{
    user-select: none;
    cursor: pointer;
}
ul li .left p:first-child{
    color: #555;
    font-size: 3.3vw;
}
ul li .left p:last-child{
    color: #333;
    font-size: 4.2vw;
    font-weight: 600;
}
ul li .right{
    font-size: 4vw;
    color: #E6A23C;
}
</style>